package com.sh.suhe.utildemos.book.base;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.os.IBinder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseMvpFragment<T extends BasePresenter> extends Fragment {
    private final int MIN_CLICK_DELAY_TIME = 500;
    private long lastClickTime;
    public FragmentActivity mActivity;
    public Context mContext;
//    private CompositeDisposable mDisposable;
    public T presenter;
    private boolean repeatView = true;
    public View rootView;
    public Unbinder unbinder;

    public Fragment getFragment() {
        return this;
    }

    public abstract int getLayoutId();

    public abstract void initData();

    public abstract void initListener();

    public abstract void initView();

    public abstract void loadData();

    public abstract T setPresenter();

    @Nullable
    public View onCreateView(@NonNull LayoutInflater layoutInflater, @Nullable ViewGroup viewGroup, @Nullable Bundle bundle) {
        if (this.rootView == null) {
            this.rootView = layoutInflater.inflate(getLayoutId(), viewGroup, false);
        }
        this.unbinder = ButterKnife.bind(getFragment(), this.rootView);
        this.presenter = setPresenter();
        this.mActivity = getActivity();
        this.mContext = getContext();
        return this.rootView;
    }

    public void onViewCreated(@NonNull View view, @Nullable Bundle bundle) {
        BaseMvpFragment.super.onViewCreated(view, bundle);
        if (this.repeatView) {
            this.repeatView = false;
            initData();
            initView();
            initListener();
            loadData();
        }
    }

    public void onDestroyView() {
        BaseMvpFragment.super.onDestroyView();
        this.unbinder.unbind();
    }

    public boolean isFastClick() {
        long currentTimeMillis = System.currentTimeMillis();
        boolean z = currentTimeMillis - this.lastClickTime >= 500;
        this.lastClickTime = currentTimeMillis;
        return z;
    }

    /* access modifiers changed from: protected */
//    public void addDisposable(Disposable disposable) {
//        if (this.mDisposable == null) {
//            this.mDisposable = new CompositeDisposable();
//        }
//        this.mDisposable.add(disposable);
//    }

    public void onDestroy() {
        BaseMvpFragment.super.onDestroy();
        if (this.presenter != null) {
            this.presenter.onDestroy();
            this.presenter = null;
        }
//        if (this.mDisposable != null) {
//            this.mDisposable.dispose();
//        }
    }

//    public void finishRefresh(SmartRefreshLayout smartRefreshLayout) {
//        if (smartRefreshLayout != null) {
//            if (smartRefreshLayout.isLoading()) {
//                smartRefreshLayout.finishLoadMore();
//            }
//            if (smartRefreshLayout.isRefreshing()) {
//                smartRefreshLayout.finishRefresh();
//            }
//        }
//    }

    public void hideKeyboard(IBinder iBinder) {
        if (iBinder != null) {
            @SuppressLint("WrongConstant") InputMethodManager inputMethodManager = (InputMethodManager) this.mContext.getSystemService("input_method");
            if (inputMethodManager != null) {
                inputMethodManager.hideSoftInputFromWindow(iBinder, 2);
            }
        }
    }

    public void openKeyboard(EditText editText) {
        @SuppressLint("WrongConstant") InputMethodManager inputMethodManager = (InputMethodManager) this.mContext.getSystemService("input_method");
        if (inputMethodManager != null) {
            inputMethodManager.showSoftInput(editText, 1);
        }
    }

    public int getStatusBarHeight() {
        int identifier = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (identifier > 0) {
            return getResources().getDimensionPixelSize(identifier);
        }
        return 0;
    }
}
